/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.cctv;

import com.seari.bean.cctv.AgreInfo;
import com.seari.bean.cctv.Login;
import com.seari.bean.cctv.XMLBean;
import com.seari.pa.handler.PaClientHandler;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.dom4j.Document;
import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.slf4j.LoggerFactory;
/**
 *
 * @author 29120
 */
public class ParseXmlToString {
    protected static org.slf4j.Logger logger = LoggerFactory.getLogger(ParseXmlToString.class);
    
    public String  thansferToXml (Object object) throws ParserConfigurationException{
        
         Document document=DocumentHelper.createDocument();
        if(object instanceof AgreInfo){
            
            AgreInfo ai = (AgreInfo) object;        
            Element root = document.addElement("body");
            Element infotype = root.addElement("infotype");
            Element keyvalue =root.addElement("keyvalue");
            Element state =root.addElement("state");
            Element step =root.addElement("step");
            infotype.setText(ai.getInfotype()+"");
            keyvalue.setText(ai.getKeyvalue()+"");
            state.setText(ai.getState()+"");
            step.setText(ai.getStep()+"");
        }else if (object instanceof Login) {
            
            Login lo = (Login) object;
            Element root = document.addElement("body");
            Element login = root.addElement("login");
            Element username =root.addElement("username");
            Element password =root.addElement("password");
            Element ip =root.addElement("ip");
            Element port =root.addElement("port");
            login.setText(lo.getLogin()+"");
            username.setText(lo.getUsername());
            password.setText(lo.getPassword());
            ip.setText(lo.getIp());
            port.setText(lo.getPort()+"");
        }else if(object instanceof XMLBean)
        {
            XMLBean xmlBean = (XMLBean) object;
            Element root = document.addElement("body");
            Map<String,String> attrMap = xmlBean.getAttrMap();
            for (Map.Entry<String, String> entry : attrMap.entrySet())
            {
                root.addElement(entry.getKey()).setText(entry.getValue());
            }
        }
         document.setXMLEncoding("utf-8");
         String  docString = document.asXML();
         return docString +"\r\n\r\n";

        }
      
}